package com.unisci.android.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.jar.Attributes.Name;

import com.unisci.android.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProjectItemAdapter extends BaseAdapter {

	private List<HashMap<String, String>> mData;
	private Context mContext;
	private LayoutInflater mInflater;
	private String[] mKeys;
	private int mLayout;
	private int[] mViewIds;
	private final ArrayList<Integer> mBackgroundColors;

	public ProjectItemAdapter(Context context,
			List<HashMap<String, String>> data, int layout, String[] keys,
			int[] viewIds) {
		mContext = context;
		mData = data;
		mInflater = LayoutInflater.from(context);
		mKeys = keys;
		mLayout = layout;
		mViewIds = viewIds;
		mBackgroundColors = new ArrayList<Integer>();
		mBackgroundColors.add(R.color.orange2);
		mBackgroundColors.add(R.color.green2);
		mBackgroundColors.add(R.color.blue2);
		mBackgroundColors.add(R.color.yellow2);
		mBackgroundColors.add(R.color.grey2);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return mData.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(mLayout, null);
			for (int i = 0; i < mKeys.length; i++) {
				if (mKeys[i].equals("name")) {
					TextView tv = (TextView) convertView
							.findViewById(mViewIds[i]);
					holder.tv_name = tv;
				} else if (mKeys[i].equals("time")) {
					TextView tv = (TextView) convertView
							.findViewById(mViewIds[i]);
					holder.tv_time = tv;
				}
			}
			holder.iv = (ImageView) convertView.findViewById(R.id.item_image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.iv.setImageResource(R.drawable.ic_project);
		for (int i = 0; i < mKeys.length; i++) {
			if (mKeys[i].equals("name")) {
				holder.tv_name.setText(mData.get(pos).get("name"));
			} else if (mKeys[i].equals("time")) {
				holder.tv_time.setText(mData.get(pos).get("time"));
			}
		}
		int backgroundIndex = pos >= mBackgroundColors.size() ? pos
				% mBackgroundColors.size() : pos;

		convertView.setBackgroundResource(mBackgroundColors
				.get(backgroundIndex));
		return convertView;
	}

	public class ViewHolder {
		public ImageView iv;
		public TextView tv_name;
		public TextView tv_time;
	}

}
